package com.hwrt.hellomaven.common;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Created by Administrator--善知  on 2018/5/18.
 */
public abstract class CommonCache {
    @Autowired
    protected RedisTemplate redisTemplate;

    public void setOneBean(Object bean, String key) {
        redisTemplate.opsForValue().set(key, JSON.toJSONString(bean));
    }

    public  <T> T getOneBean(Class<T> clazz,String key) {
        Object o = redisTemplate.opsForValue().get(key);
        if (o==null) {
            return null;
        }
        String json=o.toString();

        return JSON.parseObject(json,clazz);
    }


    /*
        删除指定key数据
     */
    public void deleteOneData(String key){
        redisTemplate.delete(key);

    }


}
